package recursion;

public class PascalTriangleMoitation {
    public static int getNum(int row, int column, int[][] cache) {
        if (cache[row][column] > 0) { // 数组初始值为0 不为0则返回值
            return cache[row][column];
        }
        if (column == 0 || row == column) { // 当处于第一列 以及行列相等时 值为1
            cache[row][column] = 1;
            return 1;
        }
        cache[row][column] = getNum(row - 1, column, cache) + getNum(row - 1, column - 1, cache);
        return cache[row][column];
    }

    public static void printTriangle(int num) {
        int[][] cache = new int[num][]; // 设定外层创建多少个数组
        for (int row = 0; row < num; row++) {
            cache[row] = new int[row + 1]; // 初始化数组 这个容易忘记
            printSpace(num, row);
            for (int column = 0; column <= row; column++) {
                System.out.printf("%4d", getNum(row, column, cache));
            }
            System.out.println();
        }
    }

    public static void printSpace(int num, int row) {
        int count = ((num - row - 1) << 1);
        for (int i = 0; i < count; i++) {
            System.out.print(" ");
        }
    }

    public static void main(String[] args) {
        printTriangle(50);
    }
}
